<?php


class CrondIndex extends CrondBase
{

    // 整个任务列表开始执行时间
    private $time_start = 0;


    public function __construct()
    {
        parent::__construct();
        $this->time_start = microtime(true);

    }


    /**
     * @param $cli_data
     */
    public function index($cli_data){
        if (php_sapi_name() !== 'cli') {
            throw new Exception("不是cli模式运行");
        }

        // 临时设置最大内存3G
        ini_set('memory_limit','3072M');

        if (isset($cli_data[1])){
            log::logs("cli 运行任务","crond/CrondIndex");
            $crond_data = $cli_data[1];
            $crond_data = explode("&",$crond_data);

            $ct   = explode("=",$crond_data[0])[1];
            $ac   = explode("=",$crond_data[1])[1];
            $date = isset($crond_data[2])?explode("=",$crond_data[2])[1]:date("Y-m-d");

            if ($ct !== "Crond"){
                echo "目前只执行Crond模式\n";
                return false;
            }

            self::run($ac,$date);
        }
    }


    public function run($ac,$date){
        $mod_file = __DIR__."/{$ac}.php";
        if (!file_exists($mod_file)){
            echo "当前执行文件不存在\n";
            return false;
        }


        if ($ac !== "CrondIndex"){
            log::logs("cli 单独运行任务{$ac}","crond/CrondIndex");
            $runtime_start = microtime(true);

            require_once __DIR__."/{$ac}.php";
            $model = new $ac();
            $model->index($date);

            $runtime = microtime(true) - $runtime_start;
            log::logs("文件:{$ac}.php, 执行时间: {$runtime}.","crond/CrondIndex");
            return true;
        }

    }

}